<?php
class Manage extends CI_Controller
{
    private $_date;
	
	function __construct()
	{
	    parent::__construct();
	    $this->load->helper(array('url','form','date'));
	    /* 创建一个日历 */
	  $this->_date = getMyDate($this->test());
	    
	}
	
	public function index()
	{
		$this->load->library("googchart");
		$color = array('#99c754', '#54c7c5', '#999999');
		
		$dataTimeLine = array(
		             '2013' => array(
                               'jan' => 50,
		                       'feb' => 30,
		                       'mar' => 60,
		                       'jun' => 90,
		                       'jul' => 70,
		                       'aug' => 90,
		                       'sep' => 80,
		                       'okt' => 100,
		                       'nov' => 60,
		                       'dec' => 90,
		                        )
		                );
		$this->googchart->setChartAttrs(
		                                 array(
		                                 'type'=>'sparkline',
		                                 'title' => 'site visits 2013',
		                                 'data' => $dataTimeLine,
		                                 'size'=>array(700,220),
		                                 'color'=>$color,
		                                 'labelsXY'=>true,
		                                 'fill' =>array('#eeeeee','#aaaaaa'),
		                                 )
		);
		$data['visitschart'] = $this->googchart;                

		$data['mydate'] = $this->_date;

		$this->load->view('manage/header');
		$this->load->view('manage/mainboard',$data);
		$this->load->view('manage/footer');
	}
	
	public function config()
	{
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('site_title','title','required');
		if($this->form_validation->run() == false)
		{
		  $res = $this->db->select("*")->get('config')->result_array();
	      $config = array();
	      foreach($res as $row){
	      	$config[$row['name']] = $row['val'];
	      }
		  $data['config'] = $config;
	     
		  $this->load->view('manage/header');
		  $this->load->view('manage/config',$data);
		  $this->load->view('manage/footer');
		}else{
			$post = $this->input->post();
			foreach($post as $key=>$val)
			{
				$data = array("name"=>$key,"val"=>$val);
				$this->db->insert('config',$data);
			}
			redirect('manage/index');
		}
	}
	
	public function dbbackups()
	{
		 $this->load->library('dbbackups');
		 
		 $dir = scandir($this->dbbackups->_savepath);
		 	 
         $data['file_arr'] = $dir;
         $data['count_page']=0;
         $data['savedir'] = $this->dbbackups->_savedir;
		 
         $this->load->view('manage/header');
         $this->load->view('manage/dbbackup',$data);
         $this->load->view('manage/footer');
		 
	}
	
	public function backup()
	{
		$this->load->library('dbbackups');
		
		$this->dbbackups->backup();
		//$this->dbbackups->listtable();
		redirect('manage/dbbackups');
	}
	
	public function event()
	{
		/*     分页代码          */
	    $num = 4;    //每页显示数量
		if($this->uri->segment(3) == FALSE)
		{
			$offset = 0;
		}else{
			if($this->uri->segment(3) ==1)
			{
				$offset=0;
			}else{
			$offset = $this->uri->segment(3)*$num-$num;   //计算篇移量
			} 
		}
		$counts = $this->db->count_all('event');
	    $count_page = ceil($counts/$num);
        $data['count_page'] = $count_page;  
       
	    $this->db->select('*');
	    $this->db->limit($num, $offset);   
	    $this->db->order_by('eid','desc');
		
		$res = $this->db->get('event')->result_array();
		if(!$res){
			redirect('manage/index');
		}
		$data['events'] = $res;

		$this->load->view('manage/header');
		$this->load->view('event/index',$data);
		$this->load->view('manage/footer');
	}
	
	public function eventPost()
	{
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('title','title','required');
		$this->form_validation->set_rules('body','body','required');
		
		if($this->form_validation->run() == false){
				$this->load->view('manage/header');
				$this->load->view('event/post');
				$this->load->view('manage/footer');
		}else{
			    $this->load->model('model_event');
			    $post = $this->input->post();
			    $this->model_event->save($post);
			    redirect('manage/event');
		}
	}
	
	public function eventEdit()
	{
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('title','title','required');
		$this->form_validation->set_rules('body','body','required');
		
		if($this->form_validation->run() == false){
				
			$eid = $this->uri->segment(3);
				if(!$eid){
					echo "param error"; //其实是多余的
					exit();
					redirect('manage/event');
				}
				
		    $res = $this->db->select('*')->where('eid',$eid)->get('event')->result_array();
				
				if(!$res){
					echo "result error";
					exit();
					redirect('manage/event');
				}
				
		     $data['event'] = $res[0];
					
			 $this->load->view('manage/header');
			 $this->load->view('event/edit',$data);
			 $this->load->view('manage/footer');
		}else{
			$this->load->model('model_event');
			$post = $this->input->post();
			$eid = intval($post['eid']);
			$this->model_event->update($post,$eid);
			redirect('manage/event');
		}
		
	}
	
	public function eventStatus()
	{
		$eid = $this->uri->segment(3);
		
		if(!$eid){
			redirect('manage/event');
		}
		
		$eid = intval($eid);
		
		$data['status'] = 1;
		
		$this->db->where('eid',$eid)->update('event',$data);
		redirect('manage/event');
		
	}
	
	public function test()
	{
		$date_time = $this->db->select('created')->get('event')->result_array();
		
		$format_date = array();
		for($i=0;$i<count($date_time);$i++)
		{
		
			$d = date('d', $date_time[$i]['created']);
			$m = date('d', $date_time[$i]['created']);
			$y = date('d', $date_time[$i]['created']);
			$format_date[] = "$d";
		}
		return $format_date;
	}
	
	
}